package cn.tedu.medicalmanagementsystem.manager.service.impl;

import cn.tedu.medicalmanagementsystem.manager.mapper.ManagerMapper;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ManagerLoginParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ManagerUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ReviewQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.entity.Manager;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.EscortVO;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.ReviewVO;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.ServiceTrackVO;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.ServiceTypeVO;
import cn.tedu.medicalmanagementsystem.manager.service.ManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Service
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    ManagerMapper managerMapper;
    @Override
    public Manager login(ManagerLoginParam managerLoginParam) {
        log.debug("开始处理管理员登录: {}", managerLoginParam);

        Manager manager = managerMapper.selectByUsernameAndPassword(managerLoginParam);

        if (manager == null) {
            throw new RuntimeException("用户名不存在");
        }

        return manager;
    }

    @Override
    public List<ServiceTrackVO> getServiceTracks() {
        log.debug("开始查询服务跟踪数据");
        return managerMapper.selectAllServiceTracks();
    }

    @Override
    public List<ReviewVO> getReviewList() {
        log.debug("开始查询陪诊评价数据");
        return managerMapper.selectReviews();
    }

    @Override
    public void delReviews(Long reviewId) {
        log.debug("开始删除评价 ID: {}", reviewId);
        int rows = managerMapper.delReviews(reviewId);
        if (rows != 1) {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public List<ReviewVO> getReviewsByEscortId(ReviewQuery reviewQuery) {
        log.debug("开始查询陪诊员 ID: {} 的评价数据", reviewQuery);
        return managerMapper.selectReviewsById(reviewQuery);
    }

    @Override
    public List<EscortVO> getEscortByStatus(String status) {
        log.debug("开始查询状态为 {} 的陪诊员信息", status);
        return managerMapper.selectEscort(status);
    }

    @Override
    public void auditEscort(Long escortId, String status, String auditComment) {
        log.debug("开始审核陪诊员 ID: {}, 状态: {}, 审核意见: {}", escortId, status, auditComment);
        managerMapper.updateEscortStatus(escortId, status, auditComment);
    }

    @Override
    public Manager getCurrentManager(HttpSession session) {
        log.debug("开始获取当前管理员信息");
        return (Manager) session.getAttribute("manager");
    }

    @Override
    public void updatePersonal(ManagerUpdateParam managerUpdateParam) {
        log.debug("开始更新管理员个人资料");

        Manager manager = new Manager();
        // 复制更新参数
        BeanUtils.copyProperties(managerUpdateParam, manager);

        int rows = managerMapper.updatePersonal(managerUpdateParam);
        if (rows != 1) {
            throw new RuntimeException("更新失败");
        }

        log.debug("管理员更新个人资料成功");
    }

    @Override
    public List<ServiceTypeVO> getServiceTypeList() {
        log.debug("开始查询服务类型数据");
        return managerMapper.selectServiceTypes();
    }

}

